package com.skplanet.rade.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skplanet.rade.dao.StoryDAO;
import com.skplanet.rade.dao.StoryTagDAO;
import com.skplanet.rade.dao.TagDAO;
import com.skplanet.rade.domain.Story;
import com.skplanet.rade.domain.StoryTag;
import com.skplanet.rade.domain.Tag;
import com.skplanet.rade.domain.User;
import com.skplanet.rade.domain.UserTag;
import com.skplanet.rade.util.TransJSON;

@Service( value="storyService" )
@Transactional
public class StoryService {
	@Autowired
	private StoryDAO storyDAO ;
	@Autowired
	private StoryTagDAO storyTagDAO;
	
	@Autowired
	private TagService tagService;
	@Autowired
	private StoryTagService storyTagService;
	@Autowired
	private UserTagService userTagService;
	@Autowired
	private UserService userService;
	
	@Transactional(readOnly = true)
	public Story getStory(long sid){
		Story story = storyDAO.findUniqByObject("sid", sid);
		return story;
	}
	
	@Transactional
	public void updateReadCount(long sid){
		Story story = storyDAO.findUniqByObject("sid", sid);
		story.setReadcount(story.getReadcount() + 1);
	}
	
	@Transactional(readOnly = true)
	public int getStoryCount(){
		return storyDAO.getAll().size();
	}
	
	@Transactional(readOnly = true)
	public List<Story> getStoryAll() {
		return storyDAO.getAll();
	}
	
	@Transactional(readOnly = true)
	public List<Story> getStoryList(int pg){
		return storyDAO.getStoryList(pg);
	}
	
	@Transactional(readOnly = true)
	public List<Story> getStoryList(String nickname) {
		return storyDAO.findListByObject("writer", nickname);
	}
	
	@Transactional(readOnly = true)
	public List<Story> getPlaStoryList(String nickname) {
		List<Story> sList = storyDAO.findListByObject("share", "true");
		sList.removeAll(getStoryList(nickname));
		
		return sList;
	}
	
	@Transactional(readOnly = true) 
	public List<Story> getAllStoryList(List<Story> sList, String nickname) {
		sList.retainAll(storyDAO.findListByObject("share", "true"));
		
		return sList;
	}
	
	@Transactional(readOnly = true)
	public List<Story> getPlaStoryList(List<Story> sList, String nickname) {
		sList.removeAll(getStoryList(nickname));
		sList.retainAll(storyDAO.findListByObject("share", "true"));
		
		return sList;
	}
	
	@Transactional(readOnly = true)
	public List<Story> getMyStoryList(List<Story> sList, String nickname) {
		sList.retainAll(getStoryList(nickname));
		return sList;
	}
	
	@Transactional(readOnly = true)
	public List<Story> getTagStoryList(String keyword) {
		Tag tag = tagService.getTagFromKeyword(keyword);
		List<StoryTag> stList = tag.getStoryTags();
		
		List<Story> sList = new ArrayList<Story>();
		
		for(int i=0; i<stList.size(); i++) {
			if(sList.contains(stList.get(i).getStory()) == false) {
				sList.add(stList.get(i).getStory());
			}
		}
		
//		sList.retainAll(storyDAO.findListByObject("share", "true"));
		
		return sList;
	}

	@Transactional(readOnly = true)
	public List<Story> getNumStoryList(List<Story> sList, int num) {
		List<Story> rList = new ArrayList<Story>();
		
		if(sList.size() <= num) {
			return sList;
		}
		
		for(int i=sList.size(); i>sList.size()-num+1; i--) {
			rList.add(sList.get(i-1));
		}
		
		return rList;
	}
	
	@Transactional
	public void addStory(Story story){
		storyDAO.save(story);
	}
	
	@Transactional
	public void updateStory(Story story){
		Story newStory = storyDAO.findUniqByObject("sid", story.getSid());
		newStory.setContents(story.getContents());
		newStory.setTitle(story.getTitle());
		newStory.setStoryTags(story.getStoryTags());
	}
	

	@Transactional
	public void deleteStory(Story story){
		storyDAO.delete(story);
	}	
	
	@Transactional
	public void addStoryTag(Story story, String keyword){
		StoryTag storyTag = new StoryTag();
		storyTag.setKeyword(keyword);
		
		Tag tag = tagService.addStoryTag(storyTag);
		
		storyTag.addCount();
		storyTag.setTag(tag);
		tagService.update(tag);

		storyTag.setStory(story);
		story.addStoryTag(storyTag);
	}
	
	@Transactional
	public void addStoryTag2(Story story, String keyword){
		StoryTag storyTag = new StoryTag();
		storyTag.setKeyword(keyword);
		
		Tag tag = tagService.addStoryTag(storyTag);
		
		updateStory(story);
		tagService.update(tag);
		
		storyTag.addCount();
		storyTag.setTag(tag);
		tagService.update(tag);

		storyTag.setStory(story);
		story.addStoryTag(storyTag);
	}
	
	@Transactional
	public void addStoryTag2(Story story, String keyword1, String keyword2, String keyword3){
		addStoryTag(story, keyword1);
		addStoryTag(story, keyword2);
		addStoryTag(story, keyword3);
	}
	
	@Transactional
	public void removeStoryTag(Story story, String keyword1, String keyword2, String keyword3){
		List<StoryTag> stList = story.getStoryTags();
		
		for(int i=0; i<stList.size(); i++) {
			(stList.get(i).getTag().getStoryTags()).removeAll(stList);
//			(story.getStoryTags()).remove(stList.get(i));
		}
		
		for(int i=0; i<stList.size(); i++) {
			storyTagDAO.update(stList.get(i));
			storyTagDAO.delete(stList.get(i));
		}
		
		story.setStoryTags(null);
	}
	
	@Transactional
	public void addStoryTag(Story story, String keyword1, String keyword2, String keyword3){
		addStoryTag(story, keyword1);
		addStoryTag(story, keyword2);
		addStoryTag(story, keyword3);
	}
	
	@Transactional
	public boolean changeStoryTag(Story story, String keyword1, String keyword2, String keyword3) {
		List<String> stList = new ArrayList();
		for(int i=0; i<story.getStoryTags().size(); i++) {
			stList.add(story.getStoryTags().get(i).getKeyword());
		}
		
		if((stList.contains(keyword1)) == false) {
			return true;
		}
		
		if((stList.contains(keyword2)) == false) {
			return true;
		}
		
		if((stList.contains(keyword3)) == false) {
			return true;
		}
		
		return false;
	}
}
